package cn.edu.neusoft.neufooddemo.activity;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.Date;
import java.util.List;

import cn.edu.neusoft.neufooddemo.R;
import cn.edu.neusoft.neufooddemo.adapter.CartOrderAdapter;
import cn.edu.neusoft.neufooddemo.bean.OrderInfo;
import cn.edu.neusoft.neufooddemo.bean.UserInfo;
import cn.edu.neusoft.neufooddemo.common.Constants;
import cn.edu.neusoft.neufooddemo.listener.RetrofitListener;
import cn.edu.neusoft.neufooddemo.model.FoodModel;
import cn.edu.neusoft.neufooddemo.model.UserModel;

public class CartOrderActivity extends BaseActivity implements RetrofitListener{
    private Intent intent;
    private TextView purchase_username,purchase_total,purchase;
    private EditText purchase_adress;
    private ImageView purchase_return;
    private RecyclerView rv;
    private FoodModel foodModel;
    private Spinner purchase_suggesttime;
    private String stime;
    private UserModel userModel;
    private String items;
    private double d_t;
    @Override
    protected void initViews() {
        setLayout(R.layout.activity_cart_order);
        purchase_username = findViewById(R.id.purchase_username);
        purchase_total = findViewById(R.id.purchase_total);
        purchase = findViewById(R.id.purchase);
        purchase_adress =  findViewById(R.id.purchase_adress);
        purchase_return = findViewById(R.id.purchase_return);
        purchase_suggesttime =findViewById(R.id.purchase_suggesttime);
        rv=findViewById(R.id.rv);
        rv.setLayoutManager(new LinearLayoutManager(CartOrderActivity.this));
    }

    @Override
    void initEvents() {

        purchase_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String address=purchase_adress.getText().toString();
                userModel = new UserModel();
                userModel.insertOrder2(getUser_id(),
                        d_t+"",
                        stime,address,items,CartOrderActivity.this);
            }
        });
    }

    @Override
    void initData() {
        intent=getIntent();
       String items=intent.getStringExtra("items");
        System.out.println(items);
        foodModel=new FoodModel();
        foodModel.getItemsFromCart(items,this);

        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        setSpinner(purchase_suggesttime, curDate);
        showUserInfo();

    }

    private void showTotal(List<OrderInfo> list) {
        d_t=0;
        for(OrderInfo o:list){
            double p=o.getPrice();
            int n=Integer.parseInt(o.getNum());
            d_t+=p*n;
        }
        purchase_total.setText(d_t+"元");
    }

    @Override
    public void onSuccess(Object o, int flag) {
        if(flag== Constants.GETUSERBYID) {
            purchase_username.setText(((UserInfo)o).getUsername());
            purchase_adress.setText(((UserInfo)o).getAddress());
        }
        else if(flag==Constants.GETITEMSFROMCART) {
            List<OrderInfo> list=(List<OrderInfo>)o;
            CartOrderAdapter adapter = new CartOrderAdapter(CartOrderActivity.this, list, R.layout.cardview_cart_order);
            rv.setAdapter(adapter);
            items="";
            for(int i=0;i<list.size();i++)
            {
                items+=list.get(i).getItem_id();
                if(i!=list.size()-1)
                    items+=",";
            }

            showTotal(list);
        }else if(flag==Constants.INSERTORDER2)
        {
            showToast("购买成功");
            Intent intent=new Intent(CartOrderActivity.this,MyOrderActivity.class);
            startActivity(intent);
        }
    }

    @Override
    public void onFail() {
        showToast("网络错误");
    }

    public void setSpinner(Spinner sp, Date date) {
        Date[] dates = new Date[5];
        for (int i = 0; i < dates.length; i++) {
            dates[i] = new Date(date.getTime() + (long) 30 * 60 * 1000 * (i + 1));
        }
        // 建立数据源
//        String[] mItems = getResources().getStringArray(R.array.suggesttime);
        final String[] mItems = new String[dates.length - 1];
        for (int i = 0; i < mItems.length; i++) {
            mItems[i] = dates[i].getHours() + ":" + formateMinute(dates[i].getMinutes()) + "----" +
                    dates[i + 1].getHours() + ":" + formateMinute(dates[i + 1].getMinutes());
        }

// 建立Adapter并且绑定数据源
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, mItems);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//绑定 Adapter到控件
        sp.setAdapter(adapter);
        sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                                       int pos, long id) {
                stime = mItems[pos];
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
                // Another interface callback
            }
        });
    }

    public String formateMinute(int minute) {
        if (minute < 10)
            return "0" + minute;
        else
            return minute + "";
    }

    private void showUserInfo() {
        userModel = new UserModel();
        userModel.getUserById(getUser_id(),this);
    }
}
